package com.bpms.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bpms.InitServlet;
import com.bpms.common.CommonUtil;
import com.bpms.common.JdbcWorker;
import com.bpms.common.VO;
import com.bpms.common.VOList;
import com.bpms.esb.SV;
import com.bpms.report.CommonGenReport;

public class ShowHDReport extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ShowHDReport() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			int page = 1;
			if (request.getParameter("page") != null && !"".equals(request.getParameter("page").trim()))
				try {
					page = Integer.parseInt(request.getParameter("page"));
				} catch (Exception e) {
					CommonUtil.debug("ShowHDReport get Page Parameter exception", e);
				}
			String where = "";
			if (request.getParameter("where") != null && !"".equals(request.getParameter("where").trim())) {
				try {
					where = request.getParameter("where");
				} catch (Exception e) {
					CommonUtil.debug("ShowHDReport get where Parameter exception", e);
				}
			}
			VOList vol = loadData("select * from cm_djb where " + where);
			byte[] buffer = genHDReport(vol, InitServlet.RealPath, InitServlet.RealPath + "report/hdreport"+page+".jasper");
			response.setContentType("application/pdf");
			response.setCharacterEncoding("gb2312");
			response.setContentLength(buffer.length);
			ServletOutputStream ouputStream = response.getOutputStream();
			ouputStream.write(buffer, 0, buffer.length);
			ouputStream.flush();
			ouputStream.close();
		} catch (Exception e) {
			CommonUtil.debug("ShowImage exception", e);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	private byte[] genHDReport(VOList vol, String realpath, String reportfile) {

		VO vd = new VO();
		vd.setObj("volist", vol);
		VO vr = SV.g("report.hdprintreport", vd);
		CommonUtil.debug("vo return from report.hdprintreport is " + (VO) ((VOList) vr.getObj("list")).listData.get(0));

		VO p = new VO();
		p.setAttr("path", reportfile);
		CommonGenReport cgr = new CommonGenReport();
		byte[] buffer = cgr.genPdfReport(p, vr, realpath);
		CommonUtil.debug("genpdfreport pdf file size " + buffer.length);
		return buffer;
	}

	private VOList loadData(String sql) {
		try {
			JdbcWorker j = new JdbcWorker();
			VOList vol = j.readAll(new VO(), sql);
			CommonUtil.debug("volist is " + vol + " volist count:" + vol.rowCount);
			return vol;
		} catch (Exception e) {
			CommonUtil.debug("ShowReport exception", e);
		}
		return null;
	}
}
